// riderDistribution/pages/register/register.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        photo: {
            front: "",
            back: ""
        },
        nickname: "",
        phone: "",
        name: "",
        identity: "",
        tool: "",
        remark: "",
        tool: "",
        toolName: "",
        tools: [ {
            id: "1",
            value: "自行车"
        }, {
            id: "2",
            value: "三轮"
        }, {
            id: "3",
            value: "二轮"
        } ],
        features: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        if (app.globalData.user) {
            //如果直接拿到openid
            that.setData({
                openid: app.globalData.user.openid
            });
        } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                that.setData({
                    openid: res.openid
                });
            });
        }
        that.getBaseInfo();
        that.getBase();
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    m_apply: res.extra.m_apply
                });
                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
            }
        });
    },
    //获取基础信息
    getBaseInfo() {
        let that = this;
        let openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    info: res.extra,
                    nickname: res.extra.nickname
                });
            }
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    chooseTools: function(e) {
        this.setData({
            tool: this.data.tools[e.detail.value].id,
            toolName: this.data.tools[e.detail.value].value
        });
    },
    recordValue: function(e) {
        var key = e.currentTarget.dataset.name;
        this.setData({
            [key]: e.detail.value
        });
    },
    apply: function() {
        let _this = this;
        let {name: name, phone: phone, identity: identity, tool: tool, remark: remark, photo: photo} = _this.data;
        var img = [];
        img.push(photo.front);
        img.push(photo.back);
        let params = {
            name: name,
            phone: phone,
            identity: identity,
            img: img.join(","),
            tool: tool,
            remark: remark
        };
        common.request({
            endpoint: "Rider/rider/apply",
            method: "post",
            data: params,
            success: res => {
                wx.showToast({
                    icon: "none",
                    title: "已提交申请，请等待审核",
                    duration: 2e3
                });
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 0
                    });
                }, 2e3);
            }
        });
    },
    uploadImage: function(e) {
        let that = this;
        var type = e.currentTarget.dataset.type;
        let count = 1;
        wx.chooseImage({
            count: count,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success(res) {
                const tempFilePaths = res.tempFilePaths;
                wx.showLoading({
                    title: "正在上传..."
                });
                var i = 0;
                var length = tempFilePaths.length;
                that.uploadFile(tempFilePaths, i, type);
            }
        });
    },
    //上传图片
    uploadFile: function(tempFilePath, i, type) {
        var that = this;
        let domian = app.globalData.domain;
        wx.uploadFile({
            url: domian + "Order/order_close/imgfile",
            filePath: tempFilePath[i],
            name: "file",
            formData: {},
            header: {
                "Content-Type": "multipart/form-data",
                uniacid: wx.getStorageSync("uniacid")
            },
            success: function(res) {
                wx.hideLoading();
                let data = JSON.parse(res.data);
                if (type == "front") {
                    that.setData({
                        ["photo.front"]: data.Url
                    });
                } else {
                    that.setData({
                        ["photo.back"]: data.Url
                    });
                }
            },
            fail: function(res) {
                wx.hideLoading();
                wx.showModal({
                    title: "错误提示",
                    content: "上传图片失败",
                    showCancel: false,
                    success: function(res) {}
                });
            }
        });
    },
    onPageTap: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    }
});